using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class SkincareProductController : ControllerBase
    {
        private readonly ISkincareProductService _skincareProductService;

        public SkincareProductController(ISkincareProductService skincareProductService)
        {
            _skincareProductService = skincareProductService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<SkincareProduct>> GetSkincareProduct(int id)
        {
            var product = await _skincareProductService.GetSkincareProductByIdAsync(id);
            if (product == null)
            {
                return NotFound();
            }
            return product;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<SkincareProduct>>> GetSkincareProductsByUserId(int userId)
        {
            var products = await _skincareProductService.GetSkincareProductsByUserIdAsync(userId);
            return Ok(products);
        }

        [HttpGet("user/{userId}/category/{category}")]
        public async Task<ActionResult<IEnumerable<SkincareProduct>>> GetSkincareProductsByCategory(int userId, string category)
        {
            var products = await _skincareProductService.GetSkincareProductsByCategoryAsync(userId, category);
            return Ok(products);
        }

        [HttpGet("user/{userId}/lowstock")]
        public async Task<ActionResult<IEnumerable<SkincareProduct>>> GetLowStockProducts(int userId)
        {
            var products = await _skincareProductService.GetLowStockProductsAsync(userId);
            return Ok(products);
        }

        [HttpGet("user/{userId}/expiring")]
        public async Task<ActionResult<IEnumerable<SkincareProduct>>> GetExpiringProducts(int userId)
        {
            var products = await _skincareProductService.GetExpiringProductsAsync(userId);
            return Ok(products);
        }

        [HttpGet("user/{userId}/stats")]
        public async Task<ActionResult<object>> GetSkincareProductStats(int userId)
        {
            var stats = await _skincareProductService.GetSkincareProductStatsAsync(userId);
            return Ok(stats);
        }

        [HttpPost]
        public async Task<ActionResult<SkincareProduct>> CreateSkincareProduct(SkincareProduct product)
        {
            var createdProduct = await _skincareProductService.CreateSkincareProductAsync(product);
            return CreatedAtAction(nameof(GetSkincareProduct), new { id = createdProduct.Id }, createdProduct);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateSkincareProduct(int id, SkincareProduct product)
        {
            if (id != product.Id)
            {
                return BadRequest();
            }

            await _skincareProductService.UpdateSkincareProductAsync(product);
            return NoContent();
        }

        [HttpPut("{id}/stock")]
        public async Task<IActionResult> UpdateProductStock(int id, [FromBody] int stockQuantity)
        {
            await _skincareProductService.UpdateProductStockAsync(id, stockQuantity);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSkincareProduct(int id)
        {
            await _skincareProductService.DeleteSkincareProductAsync(id);
            return NoContent();
        }
    }
}